let eventBus = {};

eventBus.callbacks = {};

eventBus.$on = function(event, callback) {
  if (!this.callbacks[event]) {
    this.callbacks[event] = [];
  }
  this.callbacks[event].push(callback);
};

eventBus.$off = function(event, callback) {
  if (!this.callbacks[event]) return;
  if (!callback) {
    delete this.callbacks[event];
    return;
  }
  const index = this.callbacks[event].indexOf(callback);
  if (index !== -1) {
    this.callbacks[event].splice(index, 1);
  }
};

eventBus.$emit = function(event, data) {
  if (!this.callbacks[event]) return;
  this.callbacks[event].forEach(callback => {
    callback(data);
  });
};

module.exports = eventBus;